package com.kye.express.business.jd.after_sale.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdTagReturnModeBinding;
import com.kye.model.bean.jd.after_sale.JDReturnModesResultBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/01/2018
 * @updated : 29/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDReturnModeTagsViewBinder extends ItemViewBinder<JDReturnModesResultBean.ResultBean, JDReturnModeTagsViewBinder.ViewHolder> {

    private OnTagsItemListener onTagsItemListener;

    public JDReturnModeTagsViewBinder(OnTagsItemListener onTagsItemListener) {
        this.onTagsItemListener = onTagsItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_tag_return_mode, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDReturnModesResultBean.ResultBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdTagReturnModeBinding binding;
        private JDReturnModesResultBean.ResultBean item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.tagName)
//                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            item.setSelectd(true);
                            List<JDReturnModesResultBean.ResultBean> items = (List<JDReturnModesResultBean.ResultBean>) getAdapter().getItems();
                            Observable
                                    .just(item)
                                    .doOnNext(new Consumer<JDReturnModesResultBean.ResultBean>() {
                                        @Override
                                        public void accept(JDReturnModesResultBean.ResultBean resultBean) throws Exception {
                                            resultBean.setSelectd(true);
                                        }
                                    })
                                    .flatMap(new Function<JDReturnModesResultBean.ResultBean, Observable<JDReturnModesResultBean.ResultBean>>() {
                                        @Override
                                        public Observable<JDReturnModesResultBean.ResultBean> apply(JDReturnModesResultBean.ResultBean resultBean) throws Exception {
                                            return Observable.fromIterable(items);
                                        }
                                    })
                                    .map(new Function<JDReturnModesResultBean.ResultBean, JDReturnModesResultBean.ResultBean>() {
                                        @Override
                                        public JDReturnModesResultBean.ResultBean apply(JDReturnModesResultBean.ResultBean resultBean) throws Exception {
                                            if (!resultBean.equals(item)) {
                                                resultBean.setSelectd(false);
                                            } else {
                                                resultBean.setSelectd(true);
                                            }
                                            return resultBean;
                                        }
                                    })
                                    .toList()
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<List<JDReturnModesResultBean.ResultBean>>() {
                                        @Override
                                        public void accept(List<JDReturnModesResultBean.ResultBean> resultBean) throws Exception {
                                            itemView.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    getAdapter().notifyDataSetChanged();
                                                    if (onTagsItemListener != null) {
                                                        onTagsItemListener.onTagClicked(item);
                                                    }
                                                }
                                            });
                                        }
                                    });
                        }
                    });
        }

        public void bindData(JDReturnModesResultBean.ResultBean item) {
            this.item = item;
            binding.setData(item);
            int colorUnselected = itemView.getContext().getResources().getColor(R.color.black_666);
            int colorSelected = itemView.getContext().getResources().getColor(R.color.white);
            binding.tagName.setText(item.getName());
            binding.tagName.setTextColor(item.isSelectd() ? colorSelected : colorUnselected);
            binding.tagName.setBackgroundResource(item.isSelectd() ? R.drawable.background_round_corner_orange_0 : R.drawable.background_round_corner_white_frame_gray_0);
        }
    }

    public interface OnTagsItemListener {
        void onTagClicked(JDReturnModesResultBean.ResultBean item);
    }
}
